<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'name.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <form action=# >
    用户名：<input type="text" name="username" onblur="check(this)">
    <span id=span></span><br/>
    <input type="submit" value="提交" id="sub">
    </form>
  </body>
  <script type="text/javascript">
  	function check(obj){
  		 //alert(obj.value);
  	 //1.创建XMLHttpRequest对象
  	 	xmlhttp=null;
		if (window.XMLHttpRequest)
		  {// code for Firefox, Opera, IE7, etc.
		  xmlhttp=new XMLHttpRequest();
		  }
		else if (window.ActiveXObject)
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		//2.设置回调函数:就是服务器在请求中拿到的数据做出响应后进行的操作（自动进行的）
		xmlhttp.onreadystatechange= function() {
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				var data=xmlhttp.responseText;//获取响应的数据，以文本的形式来传输
				//var data="true";
				if(data=="true"){
					document.getElementById("span").innerHTML="<font color='green'>用户名可以使用</font>";
					document.getElementById("sub").disabled=false;
				}else if(data=="false"){
					document.getElementById("span").innerHTML="<font color='red'>用户名不可以使用</font>";
					document.getElementById("sub").disabled=true;
				}
			}
		}
		  var url="userservlet?name="+obj.value;
		  xmlhttp.open("get", url);
		  xmlhttp.send(null); 
		 
  	}
  
  </script>
</html>
